package com.pzb.web.controller.wechat;


import com.pzb.common.core.domain.AjaxResult;
import com.pzb.common.core.page.TableDataInfo;
import com.pzb.common.utils.MakeOrderNumUtil;
import com.pzb.market.domain.MkDiscount;
import com.pzb.market.service.IMkDiscountService;
import com.pzb.wechat.domain.WechatRechargeLog;
import com.pzb.wechat.service.IWechatRechargeLogService;
import com.pzb.wxpay.service.IWxPayService;
import com.pzb.wxpay.util.PayUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.pzb.common.core.controller.BaseController;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 用户账户Controller
 * 
 * @author Mr_Wu
 * @date 2022-01-23
 */
@RestController
@RequestMapping("/account")
public class WechatUserAccountController extends BaseController {
    @Autowired
    private IWxPayService wxPayService;
    @Autowired
    private IMkDiscountService discountService;
    @Autowired
    private IWechatRechargeLogService rechargeLogService;

    /**
     * 充值记录
     */
    @GetMapping("/recharge/list")
    public TableDataInfo rechargeList(@RequestParam("extAppid")String extAppid)
    {
        startPage();
        List<WechatRechargeLog> rechargeLogList = rechargeLogService.selectWechatRechargeLogList(extAppid,getOpenid());
        return getDataTable(rechargeLogList);
    }
    /**
     * 充值提交
     */
    @PostMapping("/recharge/submit")
    public AjaxResult rechargeSubmit(@RequestBody Map param, HttpServletRequest request)
    {

        String planId = param.get("planId").toString();

        String customMoney = (String) param.get("customMoney");
        String extAppid = (String) param.get("extAppid");
        String ip = PayUtil.getIpAddr(request);

        String body = "";
        String out_trade_no = MakeOrderNumUtil.makeOrderNum("BL");
        MkDiscount discount = null;
        if("0".equals(planId)){  //输入金额充值
            body = "余额充值";
        }else { //活动充值
            body = planId;
            //查询活动明细
            discount = discountService.selectMkDiscountById(Long.parseLong(planId));
            customMoney = discount.getAvailableAmount().toString();
        }
        String openid = getOpenid();
        BigDecimal b = new BigDecimal(100.00);
        int price = new BigDecimal(customMoney).multiply(b).intValue();
        //创建充值订单
        WechatRechargeLog  rechargeLog = new WechatRechargeLog();
        rechargeLog.setCreateTime(new Date());
        rechargeLog.setExtAppid(extAppid);
        rechargeLog.setOpenid(openid);
        rechargeLog.setOrderNo(out_trade_no);
        rechargeLog.setDiscountId(Long.parseLong(planId));
        rechargeLog.setPayAmount(customMoney);
        if(discount!=null){
            rechargeLog.setBalanceAmount(discount.getAvailableAmount().add(discount.getDeductionAmount()).toString());
        }else {
            rechargeLog.setBalanceAmount(customMoney);
        }
        rechargeLogService.insertWechatRechargeLog(rechargeLog);
        //统一下单  需要参数  body订单描述  out_trade_no订单号 ip下单ip price 订单金额 openId
        Map map = wxPayService.pay(extAppid,openid,ip,body,out_trade_no,price+"");
        return AjaxResult.success(map);
    }

}
